package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.HorderDao;
import dao.RouteDao;
import dao.StrategyDao;
import model.Route;
import model.Strategy;
import util.CharSet;

/**
 * Servlet implementation class RouteServlet
 */
@WebServlet("/RouteServlet")
public class RouteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RouteServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String handle = request.getParameter("handle");
		System.out.println(handle);
		if (handle != null && handle.equals("delRoute")) {
			String rname = CharSet.getNewString(request.getParameter("rname"));
			try {
				if (RouteDao.getRouteNoCacheForBool(rname)) {
					Route route = new Route();
					route = RouteDao.getRouteNoCache(rname);
					RouteDao.delRoute(route);
					Strategy strategy = StrategyDao.getStrategyNoCache(route.getRname());
					StrategyDao.delStrategy(strategy);
					if (!RouteDao.getRouteNoCacheForBool(rname)) {
						String flag = "1";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
					} else {
						String flag = "0";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
					}
				} else {
					// System.out.println("ggg");
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (handle != null && handle.equals("updateRoute")) {
			String rcname = request.getParameter("rcname");
			
			String rname = util.GetWord.getPingYin(rcname);

			String days = CharSet.getNewString(request.getParameter("days"));
			String info = request.getParameter("info");
			System.out.println("info = " + info);
			try {
				if (RouteDao.getRouteNoCacheForBool(rname)) {
					System.out.println("hah");
					Route route = new Route();
					route = RouteDao.getRouteNoCache(rname);
					route.setDays(Integer.parseInt(days));

					route.setInfo(info);
					RouteDao.updateRoute(route);
					String flag = "5";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				} else {
					String flag = "4";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				}
			} catch (Exception e) {
				String flag = "4";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (handle != null && handle.equals("addRoute")) {
			String rcname = request.getParameter("rcname");
			String rname = util.GetWord.getPingYin(rcname);
			String money = CharSet.getNewString(request.getParameter("money"));
			String days = CharSet.getNewString(request.getParameter("days"));
			String info = request.getParameter("info");
			System.out.println("hah");
			try {
				if (!RouteDao.getRouteNoCacheForBool(rname)) {
					Route route = new Route();
					route.setRcname(rcname);
					route.setRname(rname);
					route.setDays(Integer.parseInt(days));
					route.setMoney(0);
					route.setInfo(info);
					RouteDao.addRoute(route);
					
					Strategy strategy = new Strategy();
					strategy.setName(route.getRname());
					strategy.setInfo("<p><img title=\"1558581947477036701.png\" alt=\"1.png\" src=\"/Tourism/ueditor/jsp/upload/image/20190523/1558581947477036701.png\"/></p>");

					StrategyDao.addStrategy(strategy);
					
					if (RouteDao.getRouteNoCacheForBool(rname)) {
						String flag = "7";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
					} else {
						String flag = "6";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
					}

				} else {
					String flag = "6";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				}
			} catch (Exception e) {
				String flag = "6";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("/admin/AdminRouteList.jsp").forward(request, response);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
